#熟悉 urllib 模块的功能
from urllib import request,parse
import json,re
import webbrowser

#参考地址 https://www.cnblogs.com/ifso/p/4707135.html

#https://erg.37.com/get_cfh_info.php?c=notice&a=columns_articles_list&cid=779&p=1&n=10
url = "https://erg.37.com/get_cfh_info.php?c=notice&a=columns_articles_list&cid=779&p=1&n=20" #37恶人谷 37fm信息

resp = request.urlopen(url) #url可以直接传 

#也可以通过Request包装后再传,包装可以自定义参数传递
url = "https://erg.37.com/get_cfh_info.php"
params = {
    'c':'notice',
    'a':'columns_articles_list',
    'cid':779,
    'p':1,
    'n':20
} #设置参数
data = parse.urlencode(params) #把参数dict编码成url参数字符串
print(type(data),data)
data = data.encode(encoding='utf_8') #把字符串参数编码成二进制数据
req = request.Request(url,data) #生成请求数据
#resp = request.urlopen(req) # 再通过urlopen获取数据

print("resp=",resp)
byteData = resp.read()
print(byteData)
#把Unicode编码的编程中文用unicode_escape编码 单纯用utf8不行
result = byteData.decode(encoding='unicode_escape') 
print("result",result[3264])
# 当json字符串中包含\r\n这些字符串时 要使用非严格模式 不然会类似 Invalid control character at: line 1 column 8363 (char 8362) 报错
# http://www.jb51.net/article/87403.htm 报错解决地址
resultJsonData = json.loads(result,strict=False) 
#print(resultJsonData)
articleList = list(resultJsonData['data']['list'])
articleList.sort(key=lambda x:(int(x['good']),int(x['comments'])),reverse=True)
dlen = len(articleList)
reTitle = re.compile(r'《.*?》')
reNum = re.compile(r"\d+号")
reDate = re.compile(r'\d+')
for i in range(dlen):
    obj = articleList[i]
    title = obj['title']
    mTitle = reTitle.search(title) #search 是从字符串开头开始搜索 直到找到匹配的项 match 方法必须是从开头就匹配才能返回
    mNum = reNum.search(title)
    mDate = reDate.search(title)
    dateStr = mDate.group() if mDate else '(日期是?)'
    title = mTitle.group() if mTitle else title
    numStr = mNum.group() if mNum else "(?号)"
    aurl = "https://erg.37.com/column-detail.html?aid={0}".format(obj['aid'])
    msg = "%s %s选手歌曲:%s 的点赞数是%s,评论数是%s 收听: %s"%(dateStr,numStr,title,str(obj['good']),str(obj['comments']),aurl)
    print(msg)
    if i==0:
        print(aurl)
        #webbrowser.open(aurl) #用默认浏览器打开指定地址的网页

print("信息条数:",dlen)